package com.gxsyh.nantie.excel.management.provider.controller;

import com.alibaba.excel.EasyExcel;
import com.gxsyh.nantie.commons.dto.ResponseResult;
import com.gxsyh.nantie.excel.management.provider.controller.dto.RegisterParam;
import com.gxsyh.nantie.excel.management.provider.controller.dto.UploadData;
import com.gxsyh.nantie.excel.management.provider.domain.TbDepartment;
import com.gxsyh.nantie.excel.management.provider.domain.TbDepartmentUser;
import com.gxsyh.nantie.excel.management.provider.domain.TbUser;
import com.gxsyh.nantie.excel.management.provider.domain.UserAndDepartment;
import com.gxsyh.nantie.excel.management.provider.listener.UploadDataListener;
import com.gxsyh.nantie.excel.management.provider.mapper.TbDepartmentUserMapper;
import com.gxsyh.nantie.excel.management.provider.mapper.TbUserMapper;
import com.gxsyh.nantie.excel.management.provider.service.TbDepartmentService;
import com.gxsyh.nantie.excel.management.provider.service.UploadDataService;
import com.gxsyh.nantie.excel.management.provider.service.UserAndDapartmentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author hua96
 * @Date 2020-02-25
 * @Time 11:53
 * @Version 1.0.0
 */
@RestController
@Transactional
public class ExcelController {
    @Resource
    TbUserMapper tbUserMapper;
    @Resource
    TbDepartmentService tbDepartmentService;
    @Resource
    TbDepartmentUserMapper tbDepartmentUserMapper;
    @Resource
    UserAndDapartmentService userAndDapartmentService;
    @Resource
    UploadDataService uploadDataService;

    /**
     * 批量下载
     * @param response
     * @throws IOException
     */
    @GetMapping("/excel/download")
    public ResponseResult<Void> download(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        int id = 1;
        UserAndDepartment userInfo = userAndDapartmentService.getUserInfo(String.valueOf(id));
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode(userInfo.getDapartname()+"人员信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), UserAndDepartment.class).sheet("第一页").doWrite(data());
        return new ResponseResult<>(200,"导出成功",null);
    }
    /**
     * 模板下载
     * @param response
     * @throws IOException
     */
    @GetMapping("/excel/downloadMould")
    public ResponseResult<Void> downloadMould(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        int id = 1;
        UserAndDepartment userInfo = userAndDapartmentService.getUserInfo(String.valueOf(id));
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode(userInfo.getDapartname()+"人员信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), UploadData.class).sheet("第一页").doWrite(dataMould());
        return new ResponseResult<>(200,"模板下载成功",null);
    }

    /**
     * 文件上传
     * <p>
     * 1. 创建excel对应的实体对象 参照{@link UploadData}
     * <p>
     * 2. 由于默认异步读取excel，所以需要创建excel一行一行的回调监听器，参照{@link UploadDataListener}
     * <p>
     * 3. 直接读即可
     */
    @PostMapping("/excel/upload")
    @ResponseBody
    public String upload(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), UploadData.class, new UploadDataListener(uploadDataService)).sheet().doRead();
        return "success";
    }

    private List data() {
        List<UserAndDepartment> userInfoList = userAndDapartmentService.getUserInfoList();
        return userInfoList;
    }
    private List dataMould() {
        UploadData u = new UploadData();
        List uu = new ArrayList();
        u.setUsername("uu");
        u.setJobNumber("uu");
        u.setName("例子");
        u.setDepartmentId(666666L);
        u.setSubsidyStatus(1);
        u.setPhone("34235");
        u.setDepartmentId(88L);
        uu.add(u);
        //System.out.println(uu);
        return uu;
    }
}
